<?php

require_once 'lib/app_config.inc';

define("SUCCESS_MESSAGE", "success");
define("ERROR_MESSAGE", "error");

function display_messages($success_msg = NULL, $error_msg = NULL) {
   echo "<div id='messages'>\n";
   if (!is_null($success_msg)) {
     display_message($success_msg, SUCCESS_MESSAGE);
   }
   if (!is_null($error_msg)) {
     display_message($error_msg, ERROR_MESSAGE);
   }
   echo "</div>\n\n";
}

function display_message($msg, $msg_type) {
  echo '<div class="' . $msg_type . '"' . PHP_EOL;
  echo '<p>' . $msg . '</p>' . PHP_EOL;
  echo '</div>' . PHP_EOL;
}

function display_head($page_title = "", $embedded_javascript = NULL, $js_src = NULL, $embedded_css = NULL, $css_src) {
  echo <<<EOD
    <!doctype html>
    <html lang="ru">
       <head>
          <meta charset="cp1251">
          <title>{$page_title}</title>

EOD;
  if (!is_null($embedded_javascript)) {
    echo '<script type="javascript">' . PHP_EOL . 
      $embedded_javascript . PHP_EOL .
      '</script>' . PHP_EOL;
  }
  if (!is_null($js_src)) {
    echo '<script src="' . $js_src . '"></script>' . PHP_EOL;
  }
  if (!is_null($embedded_css)) {
    echo '<style type="text/css">' . PHP_EOL .
      $embedded_css . PHP_EOL .
      '</style>' . PHP_EOL;
  }
  if (!is_null($css_src)) {
    echo '<style type="text/css" scr="' . $css_src . '"></style>' . PHP_EOL;
  }
  echo '</head>' . PHP_EOL;
}

function display_title($title, $success_msg = NULL, $error_msg = NULL) {
  $prog_name = PROG_NAME;
  echo <<<EOD
    <body>
       <div id="header"><h1>{$prog_name}<h1></div>
       <div id="page_title">{$title}</div>
EOD;
  echo PHP_EOL;
  display_messages($success_msg, $error_msg);
}

function page_start($title, $js = NULL, $js_src = NULL, $css = NULL,
		    $css_src = NULL, $success_msg = NULL, $error_msg = NULL) {
  display_head($title, $js, $js_src, $css, $css_src);
  display_title($title, $success_msg, $error_msg);
}
?>
